package sw6.pc.logic;

import java.awt.Color;
import java.awt.Label;
import java.io.*;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.ArrayList;

import javax.swing.*;
import javax.swing.border.LineBorder;

import Plugin.IGuiPlugin;

public class GuiBuilder {

	private static final int SETTINGS_BOX_WIDTH = 300;
	private static final int SETTINGS_BOX_HEIGHT = 100;
	private static final int MENU_WIDTH = 300;
	private static final int MENU_HEIGHT = 50;
	private static final int MARGIN = 10;
	private static final String JAR_DIR = "C:/JarDir/";
	
	public static void BuildMenu(Menu menu, String appName) throws MalformedURLException, ClassNotFoundException, InstantiationException, IllegalAccessException {
		int currentHeight = 0;
		
		// To create the JComponents for menu, the code will first loop through all the settings in the menu,
		// and generate the JComponents for each setting. All JComponents are stored in menu
		for (int i = 0; i < menu.getSettings().size(); i++) {
			
			Setting setting = menu.getSettings().get(i);
			
			// If the setting is a custom setting, we set up the gui components in a special custom way, by loading a JPanel from a GUI-plugin
			if (setting.getClassPath() != "") {
				JPanel customPanel = loadCustom(setting, appName);
				customPanel.setLocation(MARGIN, MARGIN + currentHeight);
				menu.addJComponent(customPanel);
				currentHeight += customPanel.getHeight() + MARGIN;
			}
			
			// Creates the panel containing all the JComponents for the setting
			JPanel panel = new JPanel();
			panel.setVisible(true);
			panel.setBounds(MARGIN, MARGIN + currentHeight, SETTINGS_BOX_WIDTH, SETTINGS_BOX_HEIGHT);
			panel.setLayout(null);
			panel.setBorder(new LineBorder(Color.black, 2));
		
			// Creates the name label
			JLabel label = new JLabel(setting.getRealName() + ":");
			label.setBounds(5, 5, 100, 20);
			label.setVisible(true);
			panel.add(label);
			
			// Creates the description box
			JTextArea description = new JTextArea();
			description.setText("Description: " + setting.getDescription());
			description.setVisible(true);
			description.setLineWrap(true);
			description.setBounds(5, 30, 290, 65);
			description.setEditable(false);
			description.setBorder(new LineBorder(Color.black,1));
			
			panel.add(description);
			
			// Creates the edit button
			JButton button = new JButton();
			button.setText("Rediger");
			button.setVisible(true);
			button.setBounds(195,5,100,20);
			panel.add(button);
			
			BuildSetting(setting, appName);
			menu.addJComponent(panel);
			currentHeight += SETTINGS_BOX_HEIGHT + MARGIN;
		}
		
		// This code will generate the JComponents for sub menues in menu. 
		int menuCounter = 0;
		for (int i = 0; i < menu.getMenus().size(); i++) {
			// A submenu is represented by a single JButton
			JButton button = new JButton();
			button.setVisible(true);
			button.setText(menu.getMenus().get(i).mRealName);
			button.setBounds(
					MARGIN,
					MARGIN + currentHeight + menuCounter * (MENU_HEIGHT + 10),
					MENU_WIDTH,
					MENU_HEIGHT
					);
			menu.addJComponent(button);
			
			// For each sub-menu in menu, we generate the necessesary JComponents by calling BuildMenu recursivly.
			BuildMenu(menu.getMenus().get(i), appName);
			
			menuCounter++;
		}
		
	}
	
	private static void BuildSetting(Setting setting, String appName) throws MalformedURLException, ClassNotFoundException, InstantiationException, IllegalAccessException {
		JPanel panel = new JPanel();
		
		if (setting.getClassPath() != "") {
			panel = loadCustom(setting, appName);
			setting.setJPanel(panel);
			return;
		}
		
		// Creates the input fields for the setting. How input field should be created differs according to what data type the setting is
		if (setting.mDataType == "int"  || setting.mDataType == "double") {
			// Creates the input field
			JTextField tf = new JTextField();
			tf.setBounds(235,6,60,20);
			tf.setVisible(true);
			tf.setText("-1");

			// Adds the JCompontents to the panel
			
			panel.add(tf);
		}
		else if (setting.mDataType == "string") {
			// Creates the input field
			JTextField tf = new JTextField();
			tf.setBounds(180,6,115,20);
			tf.setVisible(true);
			tf.setText("text");

			// Adds the JCompontents to the panel
			
			panel.add(tf);
		}
		else if (setting.mDataType == "boolean") {
			// Creates the input field
			JCheckBox checkBox = new JCheckBox();
			checkBox.setBounds(275,6,20,20);
			checkBox.setVisible(true);
			

			// Adds the JCompontents to the panel
			
			panel.add(checkBox);
		}
		
		setting.setJPanel(panel);
	}
	
	private static JPanel loadCustom(Setting setting, String appName) throws MalformedURLException, ClassNotFoundException, InstantiationException, IllegalAccessException {
		File file = new File(JAR_DIR + appName + ".jar");
		URL url = file.toURI().toURL();
		URL[] urls = new URL[]{url};
		ClassLoader cl = new URLClassLoader(urls);

		Class cls = cl.loadClass(setting.getClassPath());
		IGuiPlugin plugin = (IGuiPlugin)cls.newInstance();
		return plugin.getJPanel();
	}
	
}
